package com.ifreshwing.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.ifreshwing.ParkSysApplication;
import com.ifreshwing.activity.base.BaseFragment;
import com.ifreshwing.adapter.ParkingListExpireAdapter;
import com.ifreshwing.userpark.R;
import com.ifreshwing.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2017/4/26.
 */
public class ExpireFragment extends BaseFragment {
    @BindView(R.id.titleBar_text)
    TextView titleBarText;
    @BindView(R.id.parking_lst)
    ListView parkingLst;
    @BindView(R.id.coor)
    CoordinatorLayout coor;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    Unbinder unbinder;
    private View view;
    ParkingListExpireAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_expire, null, false);
        unbinder = ButterKnife.bind(this, view);
        titleBarText.setText("待到期");
        swipeRefresh.setOnRefreshListener(() -> new Handler().postDelayed(() -> {
            swipeRefresh.setRefreshing(false);
            handler.sendEmptyMessage(0);
        }, 1000));
//        parkingLst.setOnScrollListener(new AbsListView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(AbsListView view, int scrollState) {
//
//            }
//
//            @Override
//            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//                swipeRefresh.setEnabled(firstVisibleItem == 0);
//            }
//        });
        swipeRefresh.setEnabled(false);
        adapter = new ParkingListExpireAdapter(ParkSysApplication.getInstance().getExpire_lst(), getActivity(),this);
        parkingLst.setAdapter(adapter);
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        String uri = data.getStringExtra("file");
//        String pos = data.getStringExtra("pos");
//        ParkSysApplication.getInstance().getExpire_lst().get(Integer.parseInt(pos.trim())).setRegion_img(new Bitmap[]{Util.getBitmapFromPath(uri),Util.getBitmapFromPath(uri)});
//        ParkSysApplication.getInstance().getExpire_lst().get(Integer.parseInt(pos.trim())).setIllegal(true);
        adapter.notifyDataSetChanged();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ToastUtil.showSnackBar(coor, "执行了刷新事件");

        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
